# -*- codeing = utf-8 -*-
# @Time : 2021/3/2 11:57
# @Atuthor: 朱朱
# @File: student_manage.py
# @Software: PyCharm
from 学生管理系统016 import files_manage
from 学生管理系统016 import module
"""
object.__dict__
一个字典或其他类型的映射对象，用于存储对象的（可写）属性。
"""
"""
1.所有学生,不管是哪个老师的,都要放到一个列表里.
2.一个老师,有一个单独的标签,对应这个老师的学生

all_student.json
{
    'student': [
        {'s_id':'stu0001','name': '张三', 'age': 89, 'tel': '237837293','b_t':'jack'},
        {'s_id':'stu0002','name': '张三', 'age': 89, 'tel': '237837293','b_t':'tony'},
        {'s_id':'stu0003','name': '张三', 'age': 89, 'tel': '237837293''b_t':'jack'},
        {'s_id':'stu0004','name': '张三', 'age': 89, 'tel': '237837293''b_t':'tony'}
    ],
    'num': 4
}
"""

TEACHER_NAME = ''
ALL_STUDENT = ''
ALL_STUDENT_JSON = 'all_student.json'


def add_student():
    # 1.要打开所有的学生信息表allstudent.json
    studnet = files_manage.read_json(ALL_STUDENT_JSON)
    # 2.循环出所有学生信息和总数
    if not studnet:
        ALL_STUDENT = []
        num = 0
    else:
        ALL_STUDENT = studnet['student']
        num = int(studnet['num'])
    # 3.输入注册信息名称:
    while True:
        s_name = input('请输入学生姓名,允许重名:')
        s_age = input('请输入年龄:')
        s_gender = input('请输入性别:')
        s_tel = input('请输入电话:')
        s_b_t = input('请输入管理员老师:')
    # 组织学号stu001
        num+=1
        s_id = 'stu'+str(num).zfill(4) # zfill补齐0
    # 5.继续输入其他信息
    # 6.创建学生对象
        s = module.Student(s_id,s_name,s_age,s_gender,s_tel,s_b_t)
    # 7.添加学生字典
        ALL_STUDENT.append(s.__dict__) # 将对象设为可写属性
    # 组装字典
        data = {'student':ALL_STUDENT,'num':len(ALL_STUDENT)}
    # 将数据写入文件里
        files_manage.write_json(ALL_STUDENT_JSON,data)
        choice = input('添加成功!\n1.继续\n2.返回\n请选择(1-2):')
        if choice == '2':
            break

def show_student():
    print('1.查看所有学生\n2.根据姓名查找\n3.根据学号查找\n其他: 返回\n请选择:')
    # 1.读取数据
    content = files_manage.read_json(ALL_STUDENT_JSON)
    # 2.根据teacher查找b_t
    s_all = content.get('student')
    value = input('请输入你的选择:')
    if value == '1':
        for s_one in s_all:
            if TEACHER_NAME == s_one['b_t']:
                print(s_one)
    elif value == '2':
        name = input('请输入学生的名字')
        flag = True # 如果没有这个学生
        for s_one in s_all:
            if s_one['name'] == name and TEACHER_NAME == s_one['b_t']:
                print(s_one)
                flag = False
        if flag:
            print('没有这个学生') # 解决循环打印问题
    elif value == '3':
        sid = input('请输入学号:例如stu0003')
        for s_one in s_all:
            if s_one['s_id'] == sid and TEACHER_NAME == s_one['b_t']:
                print(s_one)
                break
        else:
            print('没有这个学生')
    else:
        print('请输入正确的选择!!')


def modify_student():
    print('modifystudent')

def delete_student_stu():
    content = files_manage.read_json(ALL_STUDENT_JSON)
    s_all = content.get('student')
    stu = input('请输入你要删除的学号:像如stu0003')
    key = 's_id'
    s_one_list = []
    num = 0
    for s_one in s_all:
        if s_one[key] == stu and TEACHER_NAME == s_one['b_t']:
            s_one_list.append(s_one)
            s_all.remove(s_one_list[0])
            num -= 1
    # 组装数据
    data = {'student': s_all, 'num': len(s_all)}
    # 重新写数据
    files_manage.write_json(ALL_STUDENT_JSON, data)
    print('删除成功')

def delete_student():
    content = files_manage.read_json(ALL_STUDENT_JSON)
    s_all = content.get('student')
    print('1.按姓名删\n2.按学号删\n其他:返回')
    value = input('请输入你的选择:')
    if value == '1':
        s_name = input('请输入你要删除的姓名')
        key = 'name'
        s_one_list = []
        num = 0
        for s_one in s_all:
            if s_one[key] == s_name and TEACHER_NAME == s_one['b_t']:
                s_one_list.append(s_one)
                if len(s_one_list) > 1:
                    print(s_name + '同学出现重名,请根据学号查询删除')
                    delete_student_stu()
                    break
                s_all.remove(s_one_list[0])
                num-=1
        # 组装数据
        data = {'student':s_all,'num':len(s_all)}
        # 重新写数据
        files_manage.write_json(ALL_STUDENT_JSON,data)
        print('删除成功')

    elif value == '2':
        delete_student_stu()
    else:
        print('输入错误,返回')
        return




    # 要分清重名的情况,如果出现重名,再要求输入学号

def show_manager():
    content = files_manage.read_text('students_page.txt') %TEACHER_NAME
    while True:
        print(content)
        value = input('请输入你的选择1-5')
        if value == '1':
            add_student()
        elif value == '2':
            show_student()
        elif value == '3':
            modify_student()
        elif value == '4':
            delete_student()
        elif value == '5':
            break
        else:
            print('请输入正确的选择')

# add_student()
# show_student()